package com.allwees.bs.module.pms.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.module.pms.entity.Product;
import com.allwees.bs.module.pms.req.query.ProductAuditLogQueryReq;
import com.allwees.bs.module.pms.req.query.ProductQueryReq;
import com.allwees.bs.module.pms.vo.ProductAuditLogVo;
import com.allwees.core.dao.mybatis.dao.BaseDao;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Set;

/**
 *
 *
 * @author yangylsky
 * @since 2019-08-21 09:26
 *
 */
public interface ProductDao extends BaseDao<Product> {

	Page<Product> page(Page<Product> page, @Param("req") ProductQueryReq req);

	List<Product> findList(@Param("req") ProductQueryReq req);

	List<Product> findListByIdRange(@Param("startId") Integer startId, @Param("endId") Integer endId);

	List<ProductAuditLogVo> findAuditLog(@Param("req") ProductAuditLogQueryReq req);

	/**
	 * 查询存在库存预警的商品数量
	 *
	 * @return
	 */
	int countProductAlarm();

	@Select("SELECT DISTINCT custom_no FROM prod_product WHERE retailer_uuid=#{retailerUuid} AND custom_no is not null")
	List<String> findAllCustomNosByRetailerUuid(String retailerUuid);

//	@Update({"UPDATE prod_product set sku_seq=#{skuSeq} WHERE uuid=#{uuid}"})
//	void updateSkuSeq(String uuid, String skuSeq);

	@Update({"UPDATE prod_product set def_no=#{defNo} WHERE id=#{id}"})
	void updateDefNo(long id, String defNo);

	@Update({"UPDATE prod_product set num_per_user=#{num} WHERE uuid=#{productUuid}"})
	void updateNumPerUser(Integer num, String productUuid);

	@Select("SELECT uuid FROM prod_product WHERE status=0 and is_deleted=0 and forbidden_adjust_price=1")
	Set<String> allForbiddenUuids();

	List<Product> findByUuids4PublishInfo(@Param("uuids") List<String> uuids);

	@Update({"UPDATE prod_product set score=#{score}, updated_at=now() WHERE uuid=#{uuid}"})
	void updateScore(Product product);

	@Update({"UPDATE prod_product set total_collections=total_collections+1 WHERE uuid=#{productUuid}"})
	void addCollections(String productUuid);

	@Update({"UPDATE prod_product set total_collections=total_collections-1 WHERE uuid=#{productUuid}"})
	void removeCollections(String productUuid);

	@Update({"UPDATE prod_product set total_shares=total_shares+1 WHERE uuid=#{productUuid}"})
	void addShares(String productUuid);

	@Update({"UPDATE prod_product set total_sales=total_sales+#{count} WHERE uuid=#{productUuid}"})
	void addSales(String productUuid, int count);

	@Update({"UPDATE prod_product set total_refunds=total_refunds+1 WHERE uuid=#{productUuid}"})
	void addRefunds(String productUuid, int count);

	@Update({"UPDATE prod_product set total_reviews=total_reviews+1 WHERE uuid=#{productUuid}"})
	void addReviews(String productUuid);

	@Update({"UPDATE prod_product set total_views=total_views+1 WHERE uuid=#{productUuid}"})
	void addViews(String productUuid);
}
